package com.shared_file_bucket;

import com.shared_file_bucket.common.properties.FsServerProperties;
import javafx.application.Application;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;

import java.net.InetAddress;
import java.net.UnknownHostException;

@SpringBootApplication
@EnableConfigurationProperties({FsServerProperties.class})
public class SharedFileBucketApplication {

    private static String url = null;
    private static ConfigurableApplicationContext applicationContext;

    public static void main(String[] args) throws UnknownHostException {
        // 启动 Spring Boot 应用
        applicationContext = SpringApplication.run(SharedFileBucketApplication.class, args);

        System.out.println("   _____ _    _  _____ _____ ______  _____ _____ \n" +
                "  / ____| |  | |/ ____/ ____|  ____|/ ____/ ____|\n" +
                " | (___ | |  | | |   | |    | |__  | (___| (___  \n" +
                "  \\___ \\| |  | | |   | |    |  __|  \\___ \\\\___ \\ \n" +
                "  ____) | |__| | |___| |____| |____ ____) |___) |\n" +
                " |_____/ \\____/ \\_____\\_____|______|_____/_____/ \n");
        System.out.println("共享文件桶-启动成功");

        InetAddress inetAddress = InetAddress.getLocalHost();
        String ipAddress = inetAddress.getHostAddress();
        Environment environment = applicationContext.getBean(Environment.class);

        String contextPath = environment.getProperty("server.servlet.context-path");
        if (StringUtils.isEmpty(contextPath)) {
            contextPath = "/";
        }
        System.out.println("访问地址: http://" + ipAddress + ":" + environment.getProperty("server.port")
                + contextPath);
        url = "http://" + ipAddress + ":" + environment.getProperty("server.port")
                + contextPath;

        // 设置 GUI 页面所需的 URL
        MainGUI.setUrl(url);

        // 启动 JavaFX GUI
        Application.launch(MainGUI.class, args);
    }

    // 在窗口关闭时关闭服务
    public static void closeApplicationContext() {
        if (applicationContext != null) {
            applicationContext.close();
        }
    }
}
